﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BabyFood.Data;
using BabyFood.Entities;

public partial class babyadmincp_ThuVienAnh_CNHinhAnh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpBoSuuTapThumbail + "/" + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
            BindCatalog.BindBoSuuTap(cboBoSuuTap);
            if (Request.QueryString["id"] != null)
            {
                BindCatalog.BindBoSuuTap(cboBoSuuTap);
                H_CatalogId.Value = Request.QueryString["id"].ToString();
                BindData(Convert.ToInt32(H_CatalogId.Value));
            }
        }
    }
    protected void BindData(int IdCategories)
    {
        using (HinhAnh ent_HinhAnh = DataRepository.HinhAnhProvider.GetById(IdCategories))
        {
            txtTomTat.Text = ent_HinhAnh.TomTat;
            txtDienGiai.Text = Server.HtmlDecode(ent_HinhAnh.DienGiai);
            cboBoSuuTap.SelectedValue = ent_HinhAnh.IdBoSuuTap.ToString();
            H_ID_BST.Value = ent_HinhAnh.IdBoSuuTap.ToString();
            H_AnhDaiDien.Value = ent_HinhAnh.AnhDaiDien;
            imgViewer.ImageUrl = ent_HinhAnh.AnhDaiDien.Length > 0 ? Common.sPathUpBoSuuTap + "/" + ent_HinhAnh.IdBoSuuTap + "/Thumb/" + ent_HinhAnh.AnhDaiDien : "~/Images/no_image.jpg";
        }
    }

    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            using (HinhAnh ent_HinhAnh = new HinhAnh() { TomTat = Common.StandardString(txtTomTat.Text), DienGiai = Server.HtmlEncode(txtDienGiai.Text) })
            {
                ent_HinhAnh.IdBoSuuTap = Convert.ToInt32(cboBoSuuTap.SelectedValue);
                ent_HinhAnh.NgayCapNhat = Common.GetServerDateTime();
                if (H_AnhDaiDien.Value.Length == 0)
                {
                    ent_HinhAnh.AnhDaiDien = "~/Images/no_image.jpg";
                }
                else
                {
                    ent_HinhAnh.AnhDaiDien = Common.StandardString(H_AnhDaiDien.Value);
                }
                if (H_CatalogId.Value == "")
                {
                    updateStatus = DataRepository.HinhAnhProvider.Insert(ent_HinhAnh);
                }
                else
                {
                    ent_HinhAnh.Id = Convert.ToInt32(H_CatalogId.Value);
                    ent_HinhAnh.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.HinhAnhProvider.Update(ent_HinhAnh);
                    BindData(Convert.ToInt32(H_CatalogId.Value));
                    cmdNhapTiep_Click(sender, e);
                }
                if (!updateStatus)
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.HINHANH_CAPNHAT_THATBAI, true);
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.HINHANH_CAPNHAT_THANHCONG, true);
                    RedirectTo("~/babyadmincp/ThuVienAnh/QLHinhAnh.aspx");
                }
            }

        }

        catch (Exception ex)
        {
            Common.WriteLog("CNHinhAnh.aspx.cs", "cmdLuu_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.HINHANH_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "";
        imgViewer.ImageUrl = "~/Images/no_image.jpg";
        H_AnhDaiDien.Value = "~/Images/no_image.jpg";
        txtDienGiai.Text = "";
        txtTomTat.Text = "";
        BindCatalog.BindBoSuuTap(cboBoSuuTap);
    }

    private void RedirectTo(string url)
    {
        string redirectURL = Page.ResolveClientUrl(url);
        string script = String.Format("window.location = '{0}';", redirectURL);
        ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", script, true);
    }
}